<?php
namespace Admin\Controller;
use Common\Model\CommonModel;
use Think\Controller;

class NewsController extends BaseController{

    /**
     *
     */
    public function  index(){

        //主页
        $newsModel=M('news');
        if(!empty($_GET['news_cate'])){
            unset($_SESSION['like']['news']);
            $_SESSION['like']['news']=$_GET['news_cate'];
            $condition1['name']=array('like',"%".$_GET['news_cate']."%");
            $newscate=M('newscate')->where($condition1)->select();
            $news_cate=array();
            foreach($newscate as $key=>$value){
                $news_cate[]=$value['id'];
            }
            if(!empty($news_cate)){
                $condition['news_cate']=array('in',$news_cate);
            }else{
                $this->display();
                exit;
            }
        }

        if($_GET['start']&&$_GET['end']){
            $condition['create']= array('between',array(strtotime($_GET['start']),strtotime($_GET['end'])));
        }
        //var_dump($condition);exit;
        $count = $newsModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$newsModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::news($list);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();

    }


    public function create(){
        if(empty($_POST)){
            $newscateModel=D('Newscate');
            $cates=$newscateModel->getAll();
            //var_dump($cates);exit;
            $this->assign('cates',$cates);
        }
        $this->display();


    }

    public function doCreate(){

        if(empty($_POST['title'])){
            $this->error('标题不能为空');
            exit;
        }
        $data['title']=$_POST['title'];
        if(empty($_POST['editor'])){
            $this->error('作者不能为空');
        }
        $data['editor']=$_POST['editor'];
        if(empty($_POST['news_cate'])){
            $this->error('标签不能为空');
        }
        $data['news_cate']=$_POST['news_cate'];
        $data['abstract']=$_POST['abstract'];
        if(empty($_POST['content'])){
            $this->error('内容不能为空');
        }
        $data['content']=$_POST['content'];
        if(!$_FILES['pic']['error']==4){
            $data['layout']=$this->upload();
        }
        $data['stick']=$_POST['stick'];
        $data['status']=$_POST['status'];
        $data['create']=time();
        //var_dump($data);
        $newsModel=D('news');
        $res=$newsModel->create($data);
        if($res){
            $this->success('添加成功',U('index'));
        }else{
            $this->error('添加失败');
        }
    }
    //修改页面
    public function update(){
        $id=$_GET['id'];
        $newsModel=D('news');
        $list=$newsModel->finded('id='.$id);
        $newscateModel=D('Newscate');
        $cates=$newscateModel->getAll();
        $list['layout']=CommonModel::substr($list['layout']);
        //var_dump($list);
        //var_dump($cates);
        $this->assign('cates',$cates);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        if(empty($_POST['title'])){
            $this->error('标题不能为空');
            exit;
        }
        $data['title']=$_POST['title'];
        if(empty($_POST['editor'])){
            $this->error('作者不能为空');
        }
        $data['editor']=$_POST['editor'];
        if(empty($_POST['news_cate'])){
            $this->error('标签不能为空');
        }
        $data['news_cate']=$_POST['news_cate'];

        $data['abstract']=$_POST['abstract'];
        if(empty($_POST['content'])){
            $this->error('内容不能为空');
        }
        $data['content']=$_POST['content'];
        if(!$_FILES['pic']['error']==4){
            $data['layout']=$this->upload();
        }
        $data['stick']=$_POST['stick'];
        $data['status']=$_POST['status'];
        $data['update']=time();
        $id=$_POST['id'];
        $where='id='.$id;
        $newsModel=D('news');
        $list=$newsModel->finded($where);
        $data=CommonModel::filter($data,$list);
       // var_dump($data);exit;
        $res=$newsModel->update($where,$data);
        if($res){
            $this->success('修改成功',U('index'));
        }else{
            $this->error('修改失败');
        }
    }

    //删除
    public function del(){
        $newsModel=D('news');
        $id=$_GET['id'];

        //var_dump($id);exit;
        $res= $newsModel->deleted('id='.$id);
        if($res){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败');
        }

    }

}